﻿package mortal.component.ui
{
    import __AS3__.vec.*;
    import com.gengine.core.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.text.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.game.cache.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.mvc.core.*;

    public class GConsumeBox extends GSprite
    {
        private var _label:GTextFiled;
        private var _consumeItems:Vector.<IDispose>;
        private var _consumeInfo:Vector.<ConsumeData>;
        private var _gapWide:int = 60;
        private var _size:int = 12;
        private var _isNeedDraw:Boolean;
        private var _tmBai:GTextFormat;
        private var _tmHong:GTextFormat;
        public var consumeInfoDic:Dictionary;

        public function GConsumeBox()
        {
            this._tmBai = GlobalStyle.textFormatBai.right();
            this._tmHong = GlobalStyle.textFormatHong.right();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._label = UIFactory.gTextField("", 0, 2, 0, 20, this);
            this._label.autoSize = TextFieldAutoSize.LEFT;
            this._consumeItems = new Vector.<IDispose>;
            this._consumeInfo = new Vector.<ConsumeData>;
            this.consumeInfoDic = new Dictionary();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.MoneyUpdate, this.updateValue);
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.updateValue);
            this.clearBox();
            this._label.dispose(param1);
            this._label = null;
            this.consumeInfoDic = null;
            this._consumeItems.length = 0;
            this._consumeInfo.length = 0;
            this._size = 12;
            this._isNeedDraw = false;
            super.disposeImpl(param1);
            return;
        }// end function

        public function set label(param1:String) : void
        {
            this._label.text = param1;
            this._label.width = this._label.textWidth + 5;
            return;
        }// end function

        public function addItem(param1:String, param2:int, param3:int, param4:String = "", param5:String = "", param6:String = "") : void
        {
            var _loc_7:* = new ConsumeData(param2, param3, param4);
            _loc_7.type = param1;
            _loc_7.imgUrl = param6;
            this._consumeInfo.push(_loc_7);
            this.consumeInfoDic[param5] = _loc_7;
            this._isNeedDraw = true;
            Global.instance.callLater(this.drawView);
            return;
        }// end function

        public function set gapWide(param1:int) : void
        {
            this._gapWide = param1;
            return;
        }// end function

        public function get gapWide() : int
        {
            return this._gapWide;
        }// end function

        public function set textSize(param1:int) : void
        {
            this._size = param1;
            this._isNeedDraw = true;
            Global.instance.callLater(this.drawView);
            return;
        }// end function

        private function drawView() : void
        {
            var _loc_1:GTextFormat = null;
            var _loc_2:BaseItem = null;
            var _loc_3:GTextFiled = null;
            var _loc_4:GTextFiled = null;
            var _loc_5:GSprite = null;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:ConsumeData = null;
            var _loc_9:int = 0;
            var _loc_10:int = 0;
            if (this._isNeedDraw)
            {
                this._isNeedDraw = false;
                this._consumeInfo.sort(this.sortVec);
                this.clearBox();
                _loc_6 = 0;
                _loc_7 = 0;
                _loc_9 = 0;
                while (_loc_9 < this._consumeInfo.length)
                {
                    
                    _loc_1 = this._tmBai;
                    _loc_1.size = this._size;
                    _loc_5 = UICompomentPool.getUICompoment(GSprite);
                    while (_loc_5.numChildren)
                    {
                        
                        _loc_5.removeChildAt(0);
                    }
                    this.addChild(_loc_5);
                    _loc_8 = this._consumeInfo[_loc_9];
                    if (_loc_8.type == ConsumeData.UseItemType || _loc_8.type == ConsumeData.ShowItemType)
                    {
                        _loc_4 = UIFactory.gTextField(_loc_8.title, 0, 0, 1, 20, _loc_5, _loc_1);
                        _loc_4.x = 0;
                        _loc_4.autoSize = TextFieldAutoSize.LEFT;
                        if (_loc_8.type == ConsumeData.UseItemType)
                        {
                            _loc_3 = UIFactory.gTextField(_loc_8.num.toString(), _loc_4.x + _loc_4.textWidth + 10, 0, 1, 20, _loc_5, _loc_1);
                        }
                        else
                        {
                            _loc_10 = Cache.instance.pack.backPackCache.getItemCountByItemCode(_loc_8.code);
                            _loc_3 = UIFactory.gTextField(_loc_10.toString(), _loc_4.x + _loc_4.textWidth + 10, 0, 1, 20, _loc_5, _loc_1);
                        }
                        _loc_3.autoSize = TextFieldAutoSize.LEFT;
                        _loc_2 = UICompomentPool.getUICompoment(BaseItem);
                        _loc_2.setItemStyle(18, null, 0, 0);
                        if (_loc_8.imgUrl)
                        {
                            _loc_2.source = GlobalClass.getBitmap(_loc_8.imgUrl);
                        }
                        else
                        {
                            _loc_2.itemData = new ItemData(_loc_8.code);
                        }
                        var _loc_11:Boolean = false;
                        _loc_2.isDropAble = false;
                        _loc_2.isDragAble = _loc_11;
                        _loc_2.x = _loc_3.textWidth + _loc_3.x + 5;
                        _loc_5.addChild(_loc_2);
                        NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.updateValue);
                    }
                    else if (_loc_8.type == ConsumeData.UseMoneyType || _loc_8.type == ConsumeData.ShowMoneyType)
                    {
                        _loc_4 = UIFactory.gTextField(_loc_8.title, 0, 0, 1, 20, _loc_5, _loc_1);
                        _loc_4.autoSize = TextFieldAutoSize.LEFT;
                        if (_loc_8.type == ConsumeData.UseMoneyType)
                        {
                            if (!Cache.instance.role.enoughMoney(_loc_8.code, _loc_8.num))
                            {
                                _loc_1 = this._tmHong;
                                _loc_1.size = this._size;
                            }
                            else
                            {
                                _loc_1 = this._tmBai;
                                _loc_1.size = this._size;
                            }
                            _loc_3 = UIFactory.gTextField(_loc_8.num.toString(), _loc_4.x + _loc_4.textWidth + 10, 0, 1, 20, _loc_5, _loc_1);
                        }
                        else
                        {
                            _loc_3 = UIFactory.gTextField(Cache.instance.role.getMoneyByType(_loc_8.code).toString(), _loc_4.x + _loc_4.textWidth + 10, 0, 1, 20, _loc_5, _loc_1);
                        }
                        _loc_3.autoSize = TextFieldAutoSize.LEFT;
                        _loc_2 = UICompomentPool.getUICompoment(BaseItem);
                        _loc_2.setItemStyle(18, null, 0, 0);
                        if (_loc_8.imgUrl)
                        {
                            _loc_2.source = GlobalClass.getBitmap(_loc_8.imgUrl);
                        }
                        else
                        {
                            _loc_2.source = GlobalClass.getBitmap(GameDefConfig.instance.getEPrictUnitImg(_loc_8.code));
                        }
                        _loc_2.x = _loc_3.textWidth + _loc_3.x + 5;
                        _loc_2.toolTipData = Language.getStringByParam(20370, GameDefConfig.instance.getEPrictUnitName(_loc_8.code), Cache.instance.role.getMoneyByType(_loc_8.code), GameDefConfig.instance.getEprictUnitGetWay(this._consumeInfo[_loc_9].code));
                        _loc_5.addChild(_loc_2);
                        NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.updateValue);
                    }
                    if (_loc_9 == 0)
                    {
                        _loc_5.x = this._label.x + this._label.textWidth + 15;
                    }
                    else
                    {
                        _loc_5.x = _loc_6 + _loc_7 + this._gapWide;
                    }
                    _loc_2.y = (_loc_3.height - _loc_2.height) * 0.5 + _loc_3.y - 1;
                    _loc_6 = _loc_2.x + _loc_2.width;
                    _loc_7 = _loc_5.x;
                    this._consumeItems.push(_loc_3);
                    this._consumeItems.push(_loc_2);
                    this._consumeItems.push(_loc_4);
                    this._consumeItems.push(_loc_5);
                    _loc_9++;
                }
            }
            return;
        }// end function

        public function resetValue(param1:String, param2:int, param3:int = -1) : void
        {
            (this.consumeInfoDic[param1] as ConsumeData).num = param2;
            if (param3 != -1)
            {
                (this.consumeInfoDic[param1] as ConsumeData).code = param3;
            }
            this._isNeedDraw = true;
            Global.instance.callLater(this.drawView);
            return;
        }// end function

        public function clearBox() : void
        {
            var _loc_1:IDispose = null;
            for each (_loc_1 in this._consumeItems)
            {
                
                _loc_1.dispose(true);
            }
            this._consumeItems.length = 0;
            return;
        }// end function

        protected function sortVec(param1:ConsumeData, param2:ConsumeData) : int
        {
            if (param1.type == ConsumeData.UseMoneyType && param2.type == ConsumeData.UseItemType)
            {
                return 1;
            }
            return 0;
        }// end function

        private function updateValue(param1:Object) : void
        {
            this._isNeedDraw = true;
            Global.instance.callLater(this.drawView);
            return;
        }// end function

    }
}
